import re
from django import http
from django.conf import settings
from django.contrib.auth import authenticate, login, logout
from django.core.mail import send_mail
from django.http import HttpResponseForbidden
from django.shortcuts import render, redirect
import json
from meixixi.utils.my_email import generate_verify_url
from meixixi.utils.my_loginrequired import MyLoginRequiredMixin
from django.views import View

from meixixi.utils.response_code import RETCODE
from users.models import User, Address


class UserRegisterView(View):
    def get(self,request):
        return render(request,'register.html')


    def post(self,request):
        username = request.POST.get('username')
        password = request.POST.get('pwd')
        repassword = request.POST.get('cpwd')
        phone = request.POST.get('phone')
        msg_code = request.POST.get('msg_code')
        allow = request.POST.get('allow')

        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$',username):
            return HttpResponseForbidden('用户名格式错误!')

        if not re.match(r'^[0-9A-Za-z]{8,20}$',password):
            return HttpResponseForbidden('密码格式错误!')

        if password != repassword:
            return HttpResponseForbidden('两次密码不一致!')

        if not re.match(r'^1[3-9]\d{9}$',phone):
            return HttpResponseForbidden('手机号码格式错误!')

        if allow != 'on':
            return HttpResponseForbidden('请先同意霸王条款^_^')


        User.objects.create_user(username=username,password=password,mobile=phone)

        return redirect('http://www.taobao.com')

        redis_conn = get_redis_connection('code')
        redis_sms_code = redis_conn.get('sms_code_%s'%phone)

        if not redis_sms_code:
            return HttpResponseForbidden('短信验证码已过期')

        if msg_code != redis_sms_code.decode():
            return HttpResponseForbidden('短信验证码错误')

        return redirect('/')


class UserCountView(View):
    def get(self,request, username):
        count = User.objects.filter(username=username).count()
        return http.JsonResponse({
            "count":count
        })


class UserMobileCountView(View):
    def get(self,request,mobile):
        count = User.objects.filter(mobile=mobile).count()
        return http.JsonResponse({
            'count':count
        })


class UserloginView(View):
    def get(self,request):
        return render(request,'login.html')


    def post(self,request):
        username = request.POST.get('username')
        password = request.POST.get('pwd')
        remembered = request.POST.get('remembered')

        if not all([username, password]):
            return http.HttpResponseForbidden('参数不全')

        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$',username):
            return http.HttpResponseForbidden('用户名格式错误')

        if not re.match(r'^[0-9a-zA-Z]{8,20}$',password):
            return http.HttpResponseForbidden('米码格式错误')

        user = authenticate(request, username=username, password=password)

        if not user:
            return http.HttpResponseForbidden('用户名或密码错误')

        login(request, user)
        response = redirect('/')
        if remembered == 'on':
            request.session.set_expiry(3600*24*2)
            response.set_cookie('username',user.username,3600*24*2)
        else:
            request.session.set_expiry()
            response.set_cookie("username", user.username, 0)

        return response


class UserlogoutView(View):
    def get(self,request):

        response = redirect('/')
        response.delete_cookie("username")
        request.session.flush()
        logout(request)


        return response


class UserInfoView(MyLoginRequiredMixin):
    def get(self,request):
        context = {
            "username": request.user.username,
            "mobile": request.user.mobile,
            "email": request.user.email,
            "email_active": request.user.email_active
        }
        return render(request, 'user_center_info.html', context=context)


class UserEmailView(MyLoginRequiredMixin):
    def put(self,request):
        data = request.body.decode()
        dict_data = json.loads(data)
        email = dict_data.get("email")

        if not email:
            return http.JsonResponse({"errmsg": "参数不全",
                                      "code": 5000})

        request.user.email = email
        request.user.save()
        verify_url = generate_verify_url(request.user)
        # send_mail(subject="美多商城激活链接",
        #           message=verify_url,
        #           from_email=settings.EMAIL_FROM,
                  # recipient_list=[email])
        from celery_tasks.email.tasks import send_email_url
        send_email_url.delay(verify_url, email)
        return http.JsonResponse({"errmsg": "设置成功", "code": 0})


class UserEmailVerificationView(View):

    def get(self,request):
        #1,获取参数
        token = request.GET.get("token")

        #2,校验参数
        #2,1为空校验
        if not token:
            return http.HttpResponseForbidden("token丢失")

        #2,2解密token
        user = decode_token(token)

        if not user:
            return http.HttpResponseForbidden("token失效了,重发邮件")

        #3,数据入库
        user.email_active = True
        user.save()

        #4,返回响应
        return http.HttpResponse("恭喜邮箱激活成功")


class UserAddressView(MyLoginRequiredMixin):
    def get(self,request):
        Address.objects.filter(user_id=request.user.id).all()
        addresses = request.user.addresses.filter(is_deleted=False).all()

        # 2,拼接数据
        address_list = []
        for address in addresses:
            address_dict = {
                "id": address.id,
                "receiver": address.receiver,
                "province": address.province.name,
                "city": address.city.name,
                "district": address.district.name,
                "place": address.place,
                "mobile": address.mobile,
                "tel": address.tel,
                "email": address.email,
                "province_id": address.province_id,
                "city_id": address.city_id,
                "district_id": address.district_id,
            }
            address_list.append(address_dict)

        # 3,渲染页面
        context = {
            "addresses": address_list,
            "user": request.user
        }
        return render(request, 'user_center_site.html', context=context)


class UserAddressCreateView(MyLoginRequiredMixin):
    def post(self,request):
        #1,获取参数
        dict_data = json.loads(request.body.decode())
        title = dict_data.get("title")
        receiver = dict_data.get("receiver")
        province_id = dict_data.get("province_id")
        city_id = dict_data.get("city_id")
        district_id = dict_data.get("district_id")
        place = dict_data.get("place")
        mobile = dict_data.get("mobile")
        tel = dict_data.get("tel")
        email = dict_data.get("email")

        #2,校验参数(为空校验)
        if not all([title,receiver,province_id,city_id,district_id,place,mobile,tel,email]):
            return http.JsonResponse({"errmsg":"参数不全","code":RETCODE.NODATAERR})

        #3,数据入库
        dict_data['user'] = request.user
        address = Address.objects.create(**dict_data)

        address_dict = {
                "id":address.id,
			          "title":address.title,
                "receiver":address.receiver,
                "province":address.province.name,
                "city":address.city.name,
                "district":address.district.name,
                "place":address.place,
                "mobile":address.mobile,
                "tel":address.tel,
                "email":address.email,
                "province_id":address.province_id,
                "city_id":address.city_id,
                "district_id":address.district_id,
        }

        #4,返回响应
        context = {
            "code":RETCODE.OK,
            "address":address_dict
        }
        return http.JsonResponse(context)


class UserAddressUpdateView(MyLoginRequiredMixin):
    def put(self,request,address_id):
        #1,获取参数
        dict_data = json.loads(request.body.decode())
        title = dict_data.get("title")
        receiver = dict_data.get("receiver")
        province_id = dict_data.get("province_id")
        city_id = dict_data.get("city_id")
        district_id = dict_data.get("district_id")
        place = dict_data.get("place")
        mobile = dict_data.get("mobile")
        tel = dict_data.get("tel")
        email = dict_data.get("email")

        #2,参数校验(为空校验)
        if not all([title,receiver,province_id,city_id,district_id,place,mobile,tel,email]):
            return http.JsonResponse({"errmsg":"参数不全","code":RETCODE.NODATAERR})

        #3,数据入库
        address = Address.objects.get(id=address_id)
        address.title = title
        address.receiver = receiver
        address.province_id = province_id
        address.city_id = city_id
        address.district_id = district_id
        address.place = place
        address.mobile = mobile
        address.tel = tel
        address.email = email
        address.save()

        #4,返回响应
        address_dict = {
                "id":address.id,
                "title":address.title,
                "receiver":address.receiver,
                "province":address.province.name,
                "city":address.city.name,
                "district":address.district.name,
                "place":address.place,
                "mobile":address.mobile,
                "tel":address.tel,
                "email":address.email,
                "province_id":address.province_id,
                "city_id":address.city_id,
                "district_id":address.district_id,
        }

        context = {
            "code":RETCODE.OK,
            "address":address_dict
        }
        return http.JsonResponse(context)


    def delete(self, request, address_id):
        # 1,删除地址
        address = Address.objects.get(id=address_id)
        address.is_deleted = True
        address.save()

        # 2,返回响应
        return http.JsonResponse({
            "code": RETCODE.OK
        })


class UserAddressDefaultView(MyLoginRequiredMixin):
    def put(self,request,address_id):
        #1,设置默认地址
        request.user.default_address_id = address_id

        #2,数据入库
        request.user.save()

        #3,返回响应
        return http.JsonResponse({
            "code":RETCODE.OK
        })


class UserAddressTitleView(MyLoginRequiredMixin):
    def put(self,request,address_id):
        #1,获取参数
        dict_data = json.loads(request.body.decode())
        title = dict_data.get("title")

        #2,校验参数
        if not title:
            return http.JsonResponse({"errmsg":"参数不全","code":RETCODE.NODATAERR})

        #3,数据入库
        # address = Address.objects.get(id=address_id)
        # address.title = title
        # address.save()

        # update方法的是影响的行数, 如果返回0表示没有修改
        Address.objects.filter(id=address_id).update(title=title) #和上面三句话等价

        #4,返回响应
        return http.JsonResponse({
            "code":RETCODE.OK
        })


class UserChangePasswordView(MyLoginRequiredMixin):
    def get(self,request):
        return render(request,'user_center_pass.html')

    def post(self, request):
        # 1,获取参数
        old_pwd = request.POST.get("old_pwd")
        new_pwd = request.POST.get("new_pwd")
        new_cpwd = request.POST.get("new_cpwd")

        # 2,校验参数
        # 2,1 为空校验
        if not all([old_pwd, new_pwd, new_cpwd]):
            return render(request, 'user_center_pass.html')

        # 2,2 两次新密码是否一致
        if new_cpwd != new_pwd:
            return render(request, 'user_center_pass.html')

        # 2,3 新老密码是否一样
        if old_pwd == new_pwd:
            return render(request, 'user_center_pass.html')

        # 2,4 旧密码的正确性
        if not request.user.check_password(old_pwd):
            return render(request, 'user_center_pass.html')

        # 3,数据入库
        request.user.set_password(new_pwd)
        request.user.save()

        # 4,返回响应,重定向
        return redirect('/login')